﻿namespace Web.Control.WWW.Session
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using Model.WWW_User.Teach;
    using BLF.WWW_User.Teach;
    using Lx.Web.Common.ControlsUI;
    using System.Collections.Generic;
    using System.Collections;
    using Model.WWW_User.User;
    public class AddSession : BaseControl
    {
        protected string TNO = "";
        protected string SNO = "";
        protected long CID = -1;
        protected long LID = -1;
        /// <summary>
        /// 用户信息：0学生“昵称,头像,时区” 1教师“昵称,头像,价格(格式后的)”
        /// </summary>
        protected List<string[]> UserInfoList;
        /// <summary>
        /// 用户通讯工具帐号列表：0学生的“1～6” 1教师的“1～6”
        /// </summary>
        protected List<string[]> IMList;
        /// <summary>
        /// 0学生通讯账号、1教师通讯账号
        /// </summary>
        protected string[] IMAccount;
        /// <summary>
        /// 当前course下的所有lesson列表
        /// </summary>
        protected List<LessonModel> LessonList = new List<LessonModel>();
        /// <summary>
        /// 教师语言信息
        /// </summary>
        protected TeachModel TInfo;
        /// <summary>
        /// 当前course信息
        /// </summary>
        protected CoursesModel CourseInfo;
        /// <summary>
        /// 当前用户时区跨度（用户时区－480）
        /// </summary>
        private double TZSpan;
        protected ArrayList ArrIMAccount = new ArrayList();
        protected string TolPrice = "0";
        
        protected override void OnLoad(EventArgs e)
        {
            SNO = HttpContext.Current.User.Identity.Name;
            if (SNO == "")
            {
                Page.Response.Redirect("/user/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }
            if (!string.IsNullOrEmpty(Request["tid"]))
            {
                TNO = Request["tid"].ToString();
            }
            if (TNO.Trim().Length != 10)
            {
                Response.End(); return;
            }
            if (!string.IsNullOrEmpty(Request["cid"]))
            {
                long.TryParse(Request["cid"], out CID);
            }
            if (!string.IsNullOrEmpty(Request["lid"]))
            {
                long.TryParse(Request["lid"], out LID);
            }
            ArrayList arrList = null;
            if (CID > 0)
            {
                arrList = Teach.GetAddSessionInfo(TNO, SNO, CID);
            }
            else {
                arrList = Teach.GetAddSessionInfo(TNO, SNO);
            }
            if (arrList != null && arrList.Count > 0)
            {
                UserInfoList = arrList[0] as List<string[]>;
                IMList = arrList[1] as List<string[]>;
                if (CID > 0)
                {
                    LessonList = arrList[2] as List<LessonModel>;
                    CourseInfo = arrList[3] as CoursesModel;
                }
                else {
                    TInfo = arrList[2] as TeachModel;
                }
                double.TryParse(UserInfoList[0][2], out TZSpan);
                TZSpan = TZSpan - 480;
                IMAccount = GetTextIM();
                Response.Write(string.Format("<script> var IMAccountList={0};</script>", (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(IMList)));
            }
            else {
                Response.End(); return;
            }
            base.OnLoad(e);
        }
        System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(Lx.Web.Common.UI.Globals.Language);
        /// <summary>
        /// 获取select使用语言
        /// </summary>
        /// <returns></returns>
        protected string GetSelBL()
        {
            StringBuilder strAppend = new StringBuilder();
            if (CID > 0){
                string[] arrStr = CourseInfo.BaseLanguage.Split('|');
                for (int i = arrStr.Length - 1; i >= 0; i--)
                {
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", arrStr[i], BLF.StaticData.GetLanguageText(arrStr[i])));
                }
            }
            else { 
                if(TInfo.MotherTongue.Trim()!="")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.MotherTongue, BLF.StaticData.GetLanguageText(TInfo.MotherTongue)));
                if (TInfo.MotherTongue2.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.MotherTongue2, BLF.StaticData.GetLanguageText(TInfo.MotherTongue2)));
                if (TInfo.MotherTongue3.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.MotherTongue3, BLF.StaticData.GetLanguageText(TInfo.MotherTongue3)));
                if (TInfo.MotherTongue4.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.MotherTongue4, BLF.StaticData.GetLanguageText(TInfo.MotherTongue4)));
            }
            return strAppend.ToString();
        }
        /// <summary>
        /// 获取select所教语言
        /// </summary>
        protected string GetSelTL()
        {
            StringBuilder strAppend = new StringBuilder();
            if (CID > 0)
            {
                strAppend.Append(string.Format("<option value='{0}'>{1}</option>", CourseInfo.LanguageLearn, BLF.StaticData.GetLanguageText(CourseInfo.LanguageLearn)));              
            }
            else
            {
                if (TInfo.TLang1.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.TLang1, BLF.StaticData.GetLanguageText(TInfo.TLang1)));
                if (TInfo.TLang2.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.TLang2, BLF.StaticData.GetLanguageText(TInfo.TLang2)));
                if (TInfo.TLang3.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.TLang3, BLF.StaticData.GetLanguageText(TInfo.TLang3)));
                if (TInfo.TLang4.Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}'>{1}</option>", TInfo.TLang4, BLF.StaticData.GetLanguageText(TInfo.TLang4)));
            }
            return strAppend.ToString();
        }
        
        /// <summary>
        /// 获取select日期
        /// </summary>
        /// <param name="day">天数</param>
        /// <returns></returns>
        protected string GetSelDate(int day)
        {
            StringBuilder strAppend = new StringBuilder();
            DateTime DTime = DateTime.Now.AddMinutes(TZSpan);
            DateTime dt;
            for (int i = 0; i < day; i++)
            {
                dt = DTime.AddDays((double)i);
                strAppend.Append(string.Format("<option value='{0}'>{1}</option>", dt.ToString("yyyyMMdd"), dt.ToString(TimeToString2, cultureInfo)));
            }
            return strAppend.ToString();
        }
        /// <summary>
        /// 获取select时长
        /// </summary>
        /// <param name="textCode">时长编码</param>
        /// <returns></returns>
        protected string GetSelLongTime(string textCode)
        {
            string hourText = GetText(textCode);
            int longTime=4;
            int iSel = 2;
            if (LID > 0)
            {
                foreach (LessonModel lm in LessonList)
                {
                    if (lm.LID == LID)
                    {
                        longTime = (int)lm.LongTime;
                        iSel = longTime;
                        continue;
                    }
                }
            }
            else if (CID > 0 && LessonList.Count > 0)
            {
                longTime = (int)LessonList[0].LongTime;
                iSel = longTime;
            }            
            TolPrice = ((float)((float)iSel / 2 * float.Parse(UserInfoList[1][2]))).ToString("N0");
            StringBuilder strAppend = new StringBuilder();
            for (int i = 1; i < longTime+1; i++)
            {
                strAppend.Append(string.Format("<option value='{0}' {1}>{2}</option>", i, (i == iSel ? "selected='selected'" : ""), string.Format(hourText, (float)i / 2)));
            }
            return strAppend.ToString();
        }
        private int curIM = 0;
        /// <summary>
        /// 获取select IM工具
        /// </summary>
        /// <returns></returns>
        protected string GetSelIM()
        {
            StringBuilder strAppend = new StringBuilder();
            int len = IMList[1].Length + 1;
            for (int i = 1; i < len; i++)
            {
                if (IMList[1][i-1].Trim() != "")
                    strAppend.Append(string.Format("<option value='{0}' {1}>{2}</option>", i, (i == curIM ? "selected='selected'" : ""), BLF.WWW_User.Users.User.GetIMToolText(i.ToString())));
            }
            return strAppend.ToString();
        }        
        /// <summary>
        /// 获取用户IM账号
        /// </summary>
        /// <returns></returns>
        private string[] GetTextIM()
        {
            string[] arrStr = new string[] { "", "" };
            int len = IMList[1].Length;
            int flag = 1;
            for (int i = 0; i < len; i++)
            {
                if (IMList[0][i].Trim() != "" && IMList[1][i].Trim() != "")
                {
                    curIM = i + 1;
                    arrStr = new string[] { IMList[0][i], IMList[1][i] };
                    return arrStr;
                }
                else if (IMList[1][i].Trim() != "" && flag == 1)
                {
                    curIM = i + 1;
                    arrStr = new string[] { "", IMList[1][i] };
                    flag = 0;
                }
            }
            return arrStr;
        }
    }
}